package game;

import geom.Dimension2i;
import geom.Position2i;

import java.io.InputStream;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

import various_tmp.Game;

public class Map extends TiledMap
{
  
  private static final String tile_sets_location = "data";
  
  public Dimension2i view_dimension;
  
  
  public static final int LAYER_FOREGROUND = 1;
  public static final int LAYER_BACKGROUND = 2;
  public static final int LAYER_COLLISION = 3;
  
  
  
  //private Position2i position;

  public Map(String ref, Dimension2i view_dimension) throws SlickException
  {
    super(ref);
    this.view_dimension = view_dimension;
    init();
  }
  
 
 


  private void init()
  {
    //position = new Position2i();
  }


  
  //****************************************************
  // METHODS
  public void draw(int centreX, int centreY, Graphics g)
  {
    int width = view_dimension.getWidth();
    int height = view_dimension.getHeight();
    
    int x = getRealX(centreX - width/2);
    int y = getRealY(centreY - height/2);
    
  
    
    this.render(
        (int) -x % tileWidth, 
        (int) -y % tileHeight, 
        (int) (x / tileWidth) ,
        (int) (y / tileHeight) , 
        (width /  tileWidth) +2, 
        (height / tileHeight) +2, 
        Map.LAYER_BACKGROUND,
        false
        );
    
    this.render(
        (int) -x % tileWidth, 
        (int) -y % tileHeight, 
        (int) (x / tileWidth) ,
        (int) (y / tileHeight) , 
        (width /  tileWidth) +2, 
        (height / tileHeight) +2, 
        Map.LAYER_FOREGROUND,
        false
        );
    
    this.render(
        (int) -x % tileWidth, 
        (int) -y % tileHeight, 
        (int) (x / tileWidth) ,
        (int) (y / tileHeight) , 
        (width /  tileWidth) +2, 
        (height / tileHeight) +2, 
        Map.LAYER_COLLISION,
        false
        );
  }
  
  
  //****************************************************
  // PRIVATE
  private int getRealX(int x)
  {
    if(x<0)
      x = 0;
    else if(x + this.view_dimension.getWidth() > this.getWidth() * this.getTileWidth())
      x = this.getWidth() * this.getTileWidth() - this.view_dimension.getWidth();
    
    return x;
  }
  
  private int getRealY(int y)
  {
    if(y<0)
      y = 0;
    else if(y + this.view_dimension.getHeight() > this.getHeight() * this.getTileHeight())
      y = this.getHeight() * this.getTileHeight() - this.view_dimension.getHeight();
    
    return y;
  }
  

}
